<?php
/**
 * Created by PhpStorm.
 * User: baidu
 * Date: 17/7/27
 * Time: 下午5:57
 */
namespace app\common\model;
use app\common\model\Base;


use think\Model;

class Category extends Base {
    protected static function init()
    {
        // Cate::event('before_insert',function($cate){
        //     dump($cate->pid); die;
        // });

//        Category::event('before_delete',function($Category){
//            dump($Category); die;
//            return false;
//        });
    }
     public function catetree()
     {
         $cateres=$this->order('displayorder desc')->select();
         return $this->sort($cateres);
     }
    public function sort($data,$pid=0,$level=0){
        static $arr=array();
        foreach ($data as $k => $v) {
            if($v['parent_id']==$pid){
                $v['level']=$level;
                $arr[]=$v;
                $this->sort($data,$v['cate_id'],$level+1);
            }
        }
        return $arr;
    }
    public function getchilrenid($cateid){
        $cateres=$this->select();
        return $this->_getchilrenid($cateres,$cateid);
    }

    public function _getchilrenid($cateres,$cateid){
        static $arr=array();
        foreach ($cateres as $k => $v) {
            if($v['parent_id'] == $cateid){
                $arr[]=$v['cate_id'];
                $this->_getchilrenid($cateres,$v['cate_id']);
            }
        }

        return $arr;
    }


}
